package com.zut.edu;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qmuiteam.qmui.layout.QMUIButton;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;
import com.zut.edu.http.BaseCallback;
import com.zut.edu.http.OkhttpHelper;
import com.zut.edu.utils.CameraUtils;
import com.zut.edu.utils.StringUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class PersonVerifyDetailsActivity extends AppCompatActivity implements View.OnClickListener{
    @BindView(R.id.personverifydetails_userhead)
    QMUIRadiusImageView pvduserhead;
    @BindView(R.id.personverifydetails_groupListView)
    QMUIGroupListView pvdgroupListView;
    @BindView(R.id.personverifydetails_back)
    ImageView pvdback;
    @BindView(R.id.mine_ref_btn)
    QMUIRoundButton minerefbtn;
    @BindView(R.id.mine_agree_btn)
    QMUIRoundButton mineagreebtn;
    private String post_Id;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personverifydetails);
        ButterKnife.bind(this);
        QMUIStatusBarHelper.translucent(this);
        Intent intent = getIntent();
        String data = intent.getStringExtra("data");
        post_Id = JSON.parseObject(data).getString("userId");
        initGroupListView(data);
        setListener();
    }
    private void setListener(){
        pvdback.setOnClickListener(this);
        mineagreebtn.setOnClickListener(this);
        mineagreebtn.setChangeAlphaWhenPress(true);
        minerefbtn.setOnClickListener(this);
        minerefbtn.setChangeAlphaWhenPress(true);
    }
    private QMUICommonListItemView initItem(int id, String key, String value){
        QMUICommonListItemView item = pvdgroupListView.createItemView(
                ContextCompat.getDrawable(this, id),
                key,
                value,
                QMUICommonListItemView.HORIZONTAL,
                QMUICommonListItemView.ACCESSORY_TYPE_NONE);
        return item;
    }

    private void initGroupListView(String data){

        CameraUtils.initRadiusImageView(pvduserhead,R.mipmap.user_head);

        pvduserhead.setBorderWidth(5);

        JSONObject jsonObject = JSON.parseObject(data);
        if(jsonObject.getString("userStatus").equals("0")){
            mineagreebtn.setEnabled(false);
            mineagreebtn.setBackgroundColor(getResources().getColor(R.color.qmui_config_color_gray_6));
            mineagreebtn.setTextColor(getResources().getColor(R.color.qmui_config_color_white));
            minerefbtn.setEnabled(false);
            minerefbtn.setBackgroundColor(getResources().getColor(R.color.qmui_config_color_gray_6));
            minerefbtn.setTextColor(getResources().getColor(R.color.qmui_config_color_white));
        }
        StringUtils.photoURL(PersonVerifyDetailsActivity.this,jsonObject.getString("userPhoto"),pvduserhead);
        QMUICommonListItemView item1 = initItem(R.drawable.plugin_mine_user_id,"UID",jsonObject.getString("userId"));
        QMUICommonListItemView item2 = initItem(R.drawable.plugin_left_menu_mine,"姓名",jsonObject.getString("userName"));
        QMUICommonListItemView item3 = initItem(R.drawable.plugin_mine_account,"账号",jsonObject.getString("userAccount"));
        QMUICommonListItemView item4 = initItem(R.drawable.plugin_mine_identity,"身份",jsonObject.getString("userIdentity"));
        QMUICommonListItemView item5 = initItem(R.drawable.plugin_mine_status,"状态",StringUtils.statusJudge("userStatus",jsonObject.getString("userStatus")));
        int size = QMUIDisplayHelper.dp2px(this, 20);
        QMUIGroupListView.newSection(this)
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(item1,null)
                .addItemView(item2,null)
                .addItemView(item3,null)
                .addItemView(item4,null)
                .addItemView(item5,null)
                .setMiddleSeparatorInset(QMUIDisplayHelper.dp2px(this, 20), 0)
                .addTo(pvdgroupListView);

    }

    public void onBack(View view){

        switch (view.getId()){
            case R.id.personverifydetails_back:
                onBackPressed();
                break;
            case R.id.mine_agree_btn:
                postStatus(post_Id,"0");
                Intent backIntent1 = new Intent();
                setResult(RESULT_OK,backIntent1);
                PersonVerifyDetailsActivity.this.finish();
                break;
            case R.id.mine_ref_btn:
                postStatus(post_Id,"2");
                Intent backIntent2 = new Intent();
                setResult(RESULT_OK,backIntent2);
                PersonVerifyDetailsActivity.this.finish();
                break;
            default:
                break;
        }
    }
    @Override
    public void onClick(View v) {
        onBack(v);
    }

    private void postStatus(String userId,String eventStatus){
        OkhttpHelper okhttpHelper = OkhttpHelper.getInstance();
        Map<String,Object> params = new HashMap<>(2);
        params.put("userId",userId);
        params.put("userStatus",eventStatus);
        okhttpHelper.post(StringUtils.BASE_URL+"/user/update", params, new BaseCallback<String>() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.e("interest_onFailure", String.valueOf(e));
            }

            @Override
            public void onSuccess(@NotNull Call call, @NotNull Response response, String s, int code) throws IOException {
                Log.e("onSuccess", "response.code:"+response.code()+"\nresponse.message:"+response.message()+"\nresult.code:"+code);
            }

            @Override
            public void onError(@NotNull Call call, @NotNull Response response, int code, Exception e) {
                Log.e("onError", "response.code:"+response.code()+"\nresponse.message:"+response.message()+"\nresult.code:"+code);
            }
        });
    }
}
